# -*- coding: utf-8 -*-

"""
一个练习项目，用Python操作手机上的2048游戏
同时，结合一定的算法，看看能够达到的最大分

copyright: chysun2000@hotmail.com、

此模块的目的是封装adb的操作
- 截图
- 模拟用户滑动操作：上、下、左、右
"""

from adbutils import adb
from constant import SWIPE_LEFT, SWIPE_RIGHT, SWIPE_UP, SWIPE_DOWN, SWIPE_ERROR


class AdbTool:
    def __init__(self):
        self.device = None
        self.err_info = ''
        self.operation_range_left = 0
        self.operation_range_top = 0
        self.operation_range_right = 0
        self.operation_range_bottom = 0
        self.operation_range_width = 0
        self.operation_range_height = 0

    def open_adb(self):
        devices = adb.device_list()
        if len(devices) != 1:
            return False
        else:
            self.device = devices[0]

    def is_opened(self):
        return self.device is not None

    def screen_cap(self, pic_file_name):
        if self.device is not None:
            file_on_device = '/data/local/tmp/' + pic_file_name
            command = 'screencap ' + file_on_device
            self.device.shell(command)
            self.device.sync.pull(file_on_device, pic_file_name)
            # print('capture screen => ' + pic_file_name)
            return True
        else:
            return False

    def set_operation_range(self, x, y, w, h):
        self.operation_range_left = x
        self.operation_range_top = y
        self.operation_range_right = x + w
        self.operation_range_bottom = y + h
        self.operation_range_width = w
        self.operation_range_height = h

    def user_swipe(self, swipe_direction=SWIPE_LEFT):
        if self.device is not None:
            left = top = right = down = 0
            if swipe_direction == SWIPE_LEFT:
                left = int(self.operation_range_right)
                top = int(self.operation_range_top + self.operation_range_height / 2)
                right = self.operation_range_left
                down = top
            elif swipe_direction == SWIPE_RIGHT:
                left = self.operation_range_left
                top = int(self.operation_range_top + self.operation_range_height / 2)
                right = self.operation_range_right
                down = top
            elif swipe_direction == SWIPE_UP:
                left = int(self.operation_range_left + self.operation_range_width / 2)
                top = self.operation_range_bottom
                right = left
                down = self.operation_range_top
            elif swipe_direction == SWIPE_DOWN:
                left = int(self.operation_range_left + self.operation_range_width / 2)
                top = self.operation_range_top
                right = left
                down = self.operation_range_bottom
            else:
                print('unknown operation ' + str(swipe_direction))
                return
            self.device.swipe(left, top, right, down, 0.2)

    @staticmethod
    def get_swipe_string(operation):
        if operation == SWIPE_UP:
            return 'UP'
        elif operation == SWIPE_DOWN:
            return 'DOWN'
        elif operation == SWIPE_LEFT:
            return 'LEFT'
        elif operation == SWIPE_RIGHT:
            return 'RIGHT'
        else:
            return 'UNKNOWN OR ERROR'
